import 'emotion_record.dart';

/// 情绪趋势数据模型
/// Emotion trend data model
class EmotionTrend {
  /// 日期
  /// Date
  final DateTime date;
  
  /// 情绪类型
  /// Emotion type
  final EmotionType emotionType;
  
  /// 平均情绪强度
  /// Average emotion intensity
  final double averageIntensity;
  
  /// 记录数量
  /// Record count
  final int recordCount;

  const EmotionTrend({
    required this.date,
    required this.emotionType,
    required this.averageIntensity,
    required this.recordCount,
  });

  /// 从JSON创建实例
  /// Create instance from JSON
  factory EmotionTrend.fromJson(Map<String, dynamic> json) {
    return EmotionTrend(
      date: DateTime.fromMillisecondsSinceEpoch(json['date'] as int),
      emotionType: EmotionType.fromString(json['emotion_type'] as String),
      averageIntensity: (json['average_intensity'] as num).toDouble(),
      recordCount: json['record_count'] as int,
    );
  }

  /// 转换为JSON
  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'date': date.millisecondsSinceEpoch,
      'emotion_type': emotionType.value,
      'average_intensity': averageIntensity,
      'record_count': recordCount,
    };
  }

  @override
  String toString() {
    return 'EmotionTrend(date: $date, emotionType: $emotionType, averageIntensity: $averageIntensity, recordCount: $recordCount)';
  }
}

/// 情绪分布数据模型
/// Emotion distribution data model
class EmotionDistribution {
  /// 情绪类型
  /// Emotion type
  final EmotionType emotionType;
  
  /// 出现次数
  /// Occurrence count
  final int count;
  
  /// 占比百分比
  /// Percentage
  final double percentage;

  const EmotionDistribution({
    required this.emotionType,
    required this.count,
    required this.percentage,
  });

  /// 从JSON创建实例
  /// Create instance from JSON
  factory EmotionDistribution.fromJson(Map<String, dynamic> json) {
    return EmotionDistribution(
      emotionType: EmotionType.fromString(json['emotion_type'] as String),
      count: json['count'] as int,
      percentage: (json['percentage'] as num).toDouble(),
    );
  }

  /// 转换为JSON
  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'emotion_type': emotionType.value,
      'count': count,
      'percentage': percentage,
    };
  }

  @override
  String toString() {
    return 'EmotionDistribution(emotionType: $emotionType, count: $count, percentage: $percentage)';
  }
}

/// 强度分布数据模型
/// Intensity distribution data model
class IntensityDistribution {
  /// 强度范围（例如："1-2", "3-4", "5-6", "7-8", "9-10"）
  /// Intensity range (e.g., "1-2", "3-4", "5-6", "7-8", "9-10")
  final String intensityRange;
  
  /// 该强度范围的记录数量
  /// Record count for this intensity range
  final int count;
  
  /// 占比百分比
  /// Percentage
  final double percentage;

  const IntensityDistribution({
    required this.intensityRange,
    required this.count,
    required this.percentage,
  });

  /// 从JSON创建实例
  /// Create instance from JSON
  factory IntensityDistribution.fromJson(Map<String, dynamic> json) {
    return IntensityDistribution(
      intensityRange: json['intensity_range'] as String,
      count: json['count'] as int,
      percentage: (json['percentage'] as num).toDouble(),
    );
  }

  /// 转换为JSON
  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'intensity_range': intensityRange,
      'count': count,
      'percentage': percentage,
    };
  }

  @override
  String toString() {
    return 'IntensityDistribution(intensityRange: $intensityRange, count: $count, percentage: $percentage)';
  }
}

/// 词频数据模型
/// Word frequency data model
class WordFrequency {
  /// 词汇
  /// Word
  final String word;
  
  /// 出现频次
  /// Frequency count
  final int frequency;
  
  /// 权重（用于词云大小）
  /// Weight (for word cloud size)
  final double weight;

  const WordFrequency({
    required this.word,
    required this.frequency,
    required this.weight,
  });

  /// 从JSON创建实例
  /// Create instance from JSON
  factory WordFrequency.fromJson(Map<String, dynamic> json) {
    return WordFrequency(
      word: json['word'] as String,
      frequency: json['frequency'] as int,
      weight: (json['weight'] as num).toDouble(),
    );
  }

  /// 转换为JSON
  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'word': word,
      'frequency': frequency,
      'weight': weight,
    };
  }

  @override
  String toString() {
    return 'WordFrequency(word: $word, frequency: $frequency, weight: $weight)';
  }
}

/// 日期范围数据模型
/// Date range data model
class DateRange {
  /// 开始日期
  /// Start date
  final DateTime startDate;
  
  /// 结束日期
  /// End date
  final DateTime endDate;

  const DateRange({
    required this.startDate,
    required this.endDate,
  });

  /// 创建最近N天的日期范围
  /// Create date range for recent N days
  factory DateRange.recentDays(int days) {
    final now = DateTime.now();
    final startDate = DateTime(now.year, now.month, now.day - days + 1);
    final endDate = DateTime(now.year, now.month, now.day, 23, 59, 59);
    return DateRange(startDate: startDate, endDate: endDate);
  }

  /// 创建本周的日期范围
  /// Create date range for this week
  factory DateRange.thisWeek() {
    final now = DateTime.now();
    final startOfWeek = now.subtract(Duration(days: now.weekday - 1));
    final startDate = DateTime(startOfWeek.year, startOfWeek.month, startOfWeek.day);
    final endDate = DateTime(now.year, now.month, now.day, 23, 59, 59);
    return DateRange(startDate: startDate, endDate: endDate);
  }

  /// 创建本月的日期范围
  /// Create date range for this month
  factory DateRange.thisMonth() {
    final now = DateTime.now();
    final startDate = DateTime(now.year, now.month, 1);
    final endDate = DateTime(now.year, now.month, now.day, 23, 59, 59);
    return DateRange(startDate: startDate, endDate: endDate);
  }

  /// 获取日期范围内的天数
  /// Get number of days in the date range
  int get dayCount {
    return endDate.difference(startDate).inDays + 1;
  }

  /// 检查日期是否在范围内
  /// Check if date is within range
  bool contains(DateTime date) {
    return date.isAfter(startDate.subtract(const Duration(days: 1))) &&
           date.isBefore(endDate.add(const Duration(days: 1)));
  }

  @override
  String toString() {
    return 'DateRange(startDate: $startDate, endDate: $endDate)';
  }
}

/// 统计摘要数据模型
/// Statistics summary data model
class StatisticsSummary {
  /// 总记录数
  /// Total record count
  final int totalRecords;
  
  /// 平均情绪强度
  /// Average emotion intensity
  final double averageIntensity;
  
  /// 最常见的情绪类型
  /// Most common emotion type
  final EmotionType? mostCommonEmotion;
  
  /// 连续记录天数
  /// Consecutive recording days
  final int consecutiveDays;
  
  /// 本周记录数
  /// This week's record count
  final int thisWeekRecords;
  
  /// 上周记录数
  /// Last week's record count
  final int lastWeekRecords;

  const StatisticsSummary({
    required this.totalRecords,
    required this.averageIntensity,
    this.mostCommonEmotion,
    required this.consecutiveDays,
    required this.thisWeekRecords,
    required this.lastWeekRecords,
  });

  /// 计算周环比变化
  /// Calculate week-over-week change
  double get weekOverWeekChange {
    if (lastWeekRecords == 0) return 0.0;
    return ((thisWeekRecords - lastWeekRecords) / lastWeekRecords) * 100;
  }

  /// 从JSON创建实例
  /// Create instance from JSON
  factory StatisticsSummary.fromJson(Map<String, dynamic> json) {
    return StatisticsSummary(
      totalRecords: json['total_records'] as int,
      averageIntensity: (json['average_intensity'] as num).toDouble(),
      mostCommonEmotion: json['most_common_emotion'] != null
          ? EmotionType.fromString(json['most_common_emotion'] as String)
          : null,
      consecutiveDays: json['consecutive_days'] as int,
      thisWeekRecords: json['this_week_records'] as int,
      lastWeekRecords: json['last_week_records'] as int,
    );
  }

  /// 转换为JSON
  /// Convert to JSON
  Map<String, dynamic> toJson() {
    return {
      'total_records': totalRecords,
      'average_intensity': averageIntensity,
      'most_common_emotion': mostCommonEmotion?.value,
      'consecutive_days': consecutiveDays,
      'this_week_records': thisWeekRecords,
      'last_week_records': lastWeekRecords,
      'week_over_week_change': weekOverWeekChange,
    };
  }

  @override
  String toString() {
    return 'StatisticsSummary(totalRecords: $totalRecords, averageIntensity: $averageIntensity, mostCommonEmotion: $mostCommonEmotion)';
  }
}